package com.maikang.exammall.util;

import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.util.SignUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class WeChatUtil {
    private final WxPayService wxPayService;



    /**
     * // Config 是传出用于 js sdk 用的参数
     * type Config struct {
     * 	Timestamp string `json:"timestamp"`
     * 	NonceStr  string `json:"nonceStr"`
     * 	PrePayID  string `json:"prePayId"`
     * 	SignType  string `json:"signType"`
     * 	Package   string `json:"package"`
     * 	PaySign   string `json:"paySign"`
     * }
     */

    public WxPayMpOrderResult launchSecondPay(String nonceStr, String prepayId) {
        /**
         *  only for tradeType = "JSAPI";
         *  TODO to test
         */
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        String signType = WxPayConstants.SignType.MD5;
        String appId = wxPayService.getConfig().getAppId();

        WxPayMpOrderResult payResult = WxPayMpOrderResult.builder()
                .appId(appId)
                .timeStamp(timestamp)
                .nonceStr(nonceStr)
                .packageValue("prepay_id=" + prepayId)
                .signType(signType)
                .build();
        payResult.setPaySign(SignUtils.createSign(payResult, signType, wxPayService.getConfig().getMchKey(), null));
        return payResult;
    }
}
